package com.kitty.mina.message;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.enter.RespNotifyMisc;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.i18n.I18nIdDataPool;
import com.kitty.game.i18n.P_I18nId;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.game.utils.Const;
import com.kitty.mina.cache.SessionUtils;
import org.apache.mina.core.session.IoSession;


public class MessagePusher {

    public static void pushMessage(Role role, Message message) {
        if (role == null) {
            return;
        }
        pushMessage(role.getId(), message);
    }
    public static void pushTeamMessage(int roleId, Message message) {
        if (roleId == 0) {
            return;
        }
        TeamService bean = SpringUtils.getBean(TeamService.class);
        Team team = bean.getTeam(roleId);
        if(team != null){
            for (Member member : team.getList()) {
                Role memberRole = SpringUtils.getRoleService().getOnlinePlayer(member.getRoleId());
                if(memberRole!=null){
                    pushMessage(memberRole, message);
                }
            }

        }
    }

    /**
     * 发送多个包
     */
    public static void pushMessages(Role role, Message... messages) {
        if (role == null) {
            return;
        }
        for (Message message : messages) {
            pushMessage(role.getId(), message);
        }

    }

    private static void pushMessage(long playerId, Message message) {
        IoSession session = SessionUtils.getSessionBy(playerId);
        pushMessage(session, message);
    }

    public static void pushMessage(IoSession session, Message message) {
        if (session == null || message == null || session.isClosing()) {
            return;
        }
        session.write(message);
    }

    public static void notify2Player(IoSession session, int i18nId) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            MessagePusher.pushMessage(session, new RespMsg(idResource.getContent()));
        }
    }

    public static void notify2Player(IoSession session, int i18nId, Object... args) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            String content = String.format(idResource.getContent(), args);
            MessagePusher.pushMessage(session, new RespMsg(content));
        }
    }

    public static void notify2Player(Role role, int i18nId) {
        if (role == null) {
            return;
        }
        IoSession session = SessionUtils.getSessionBy(role.getId());
        notify2Player(session, i18nId);
    }

    public static void notify2Player(Role role, int i18nId, Object... args) {
        if (role == null) {
            return;
        }
        IoSession session = SessionUtils.getSessionBy(role.getId());
        notify2Player(session, i18nId, args);
    }

    public static void notifyNumor(int i18nId, Object... args) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            String content = String.format(idResource.getContent(), args);
            SpringUtils.getBean(ChatService.class).sendNumor(content, Const.BRODCAST_MSG_TYPE_ROLE);
        }
    }

    public static void notifyMisc2Player(IoSession session, int i18nId) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            MessagePusher.pushMessage(session, new RespNotifyMisc(idResource.getContent()));
        }
    }

    public static void notifyMisc2Player(IoSession session, int i18nId, Object... args) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            String content = String.format(idResource.getContent(), args);
            MessagePusher.pushMessage(session, new RespNotifyMisc(content));
        }
    }

    public static void notifyMisc2Player(Role role, int i18nId) {
        if (role == null) {
            return;
        }
        IoSession session = SessionUtils.getSessionBy(role.getId());
        notifyMisc2Player(session, i18nId);
    }

    public static void notifyMisc2Player(Role role, int i18nId, Object... args) {
        if (role == null) {
            return;
        }
        IoSession session = SessionUtils.getSessionBy(role.getId());
        notifyMisc2Player(session, i18nId, args);
    }

    public static void notifyMiscEx2Player(IoSession session, int i18nId) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            MessagePusher.pushMessage(session, new RespNotifyMiscEx(idResource.getContent()));
        }
    }

    public static void notifyMiscEx2Player(IoSession session, int i18nId, Object... args) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            String content = String.format(idResource.getContent(), args);
            MessagePusher.pushMessage(session, new RespNotifyMiscEx(content));
        }
    }

    public static void notifyMiscEx2Player(Role role, int i18nId) {
        if (role == null) {
            return;
        }
        IoSession session = SessionUtils.getSessionBy(role.getId());
        notifyMiscEx2Player(session, i18nId);
    }

    public static void notifyMiscEx2Player(Role role, int i18nId, Object... args) {
        if (role == null) {
            return;
        }
        IoSession session = SessionUtils.getSessionBy(role.getId());
        notifyMiscEx2Player(session, i18nId, args);
    }
}

